<?php

namespace app\admin\model;

use think\Model;

class Salemanagement extends Model
{
    // 表名
    protected $name = 'salemanagement';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'saletime_text',
        'salestatus_text',
        'servicestate_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getSalestatusList()
    {
        return ['0' => __('Salestatus 0'),'1' => __('Salestatus 1')];
    }     

    public function getServicestateList()
    {
        return ['1' => __('Servicestate 1'),'2' => __('Servicestate 2'),'3' => __('Servicestate 3')];
    }     


    public function getSaletimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['saletime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSalestatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['salestatus'];
        $list = $this->getSalestatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getServicestateTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['servicestate'];
        $list = $this->getServicestateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setSaletimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
